package com.hnkywl.sts.webservice.wly.impl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.bind.DateTypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.hnkywl.sts.dao.organ.EjzhDao;
import com.hnkywl.sts.dao.xsgl.DfkxsdDao;
import com.hnkywl.sts.entity.organ.Ejzh;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.service.base.DxpzService;
import com.hnkywl.sts.service.base.WebserviceLogService;
import com.hnkywl.sts.service.organ.EjzhService;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.report.EjzhmxService;
import com.hnkywl.sts.webservice.exception.AccountBalanceException;
import com.hnkywl.sts.webservice.wly.IAccountBalance;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 14-1-6.
 * 自由金进行支付订单
 */
@Service
public class AccountBalanceService implements IAccountBalance {
    private EjzhService ejzhService;
    private EjzhmxService ejzhmxService;
    private DfkxsdDao dfkxsdDao;
    private GsjgService gsjgService;
    private EjzhDao ejzhDao;
    private WebserviceLogService webserviceLogService;

    @Override
    public String queryAccountBalance(String param) {
        String result = null;
        String exception = null;
        try {
            Gson gson = new Gson();
            StringBuilder sb = new StringBuilder();
            Map<String, Object> resultList = gson.fromJson(param, new TypeToken<Map<String, Object>>() {
            }.getType());
            this.checkQueryAccountBalance(resultList.get("memberCode").toString(),resultList.get("parentSalerCode").toString());
            result = this.ejzhService.findKyyeByKhEsbCode(resultList.get("memberCode").toString(),resultList.get("parentSalerCode").toString());
        } catch (AccountBalanceException e) {
            exception = e.getMessage();
            result = "{\"code\":\"1\",\"flag\":\"1\",\"message\":\"" + e.getMessage() + "\"}";
        } catch (Exception e) {
            exception = e.getMessage();
            result = "{\"code\":\"1\",\"flag\":\"1\",\"message\":\"接口调用异常!\"}";
        }
        webserviceLogService.saveServerLog(
                AccountBalanceService.class.getName(),
                "queryAccountBalance",
                param,
                "返回结果：" + result,
                exception,
                ""
        );
        return result;
    }

    @Override
    public String lockAccountBalance(String param, Long serialNumber) {
        String result = null;
        String exception = null;
        try {
            Gson gson = new Gson();
            StringBuilder sb = new StringBuilder();
            Map<String, Object> resultList = gson.fromJson(param, new TypeToken<Map<String, Object>>() {
            }.getType());
            this.checkAccountBalance(resultList.get("memberCode").toString(),
                    resultList.get("salerCode").toString(),
                    resultList.get("parentSalerCode").toString(),
                    serialNumber, null);//判断数据正确性
            result = ejzhService.lockAccountBalance(param, serialNumber);
        } catch (AccountBalanceException e) {
            exception = e.getMessage();
            result = "{\"code\":\"1\",\"flag\":\"1\",\"message\":\"" + e.getMessage() + "\"}";
        } catch (Exception e) {
            exception = e.getMessage();
            result = "{\"code\":\"1\",\"flag\":\"1\",\"message\":\"" + e.getMessage() + "\"}";
        }
        webserviceLogService.saveServerLog(
                AccountBalanceService.class.getName(),
                "lockAccountBalance",
                param + "  序列号:" + serialNumber,
                "返回结果：" + result,
                exception,
                ""
        );
        return result;
    }

    @Override
    public String unlockAccountBalance(String param, Long serialNumber) {
        String result = null;
        String exception = null;
        try {
            Gson gson = new Gson();
            StringBuilder sb = new StringBuilder();
            Map<String, Object> resultList = gson.fromJson(param, new TypeToken<Map<String, Object>>() {
            }.getType());
            this.checkAccountBalance(resultList.get("memberCode").toString(),
                    resultList.get("salerCode").toString(),
                    resultList.get("parentSalerCode").toString(),
                    null,
                    serialNumber);
            result = this.ejzhService.unlockAccountBalance(param, serialNumber);
        } catch (AccountBalanceException e) {
            exception = e.getMessage();
            result = "{\"code\":\"1\",\"flag\":\"1\",\"message\":\"" + e.getMessage() + "\"}";
        } catch (Exception e) {
            exception = e.getMessage();
            result = "{\"code\":\"1\",\"flag\":\"1\",\"message\":\"接口调用异常!\"}";
        }
        webserviceLogService.saveServerLog(
                AccountBalanceService.class.getName(),
                "unlockAccountBalance",
                param + "  序列号:" + serialNumber,
                "返回结果：" + result,
                exception,
                ""
        );
        return result;
    }


    @Override
    public String accountDetailsQuery(String param) {
        String result = null;
        String exception = null;
        try {
            DateFormat dim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").registerTypeAdapter(Date.class, new DateTypeAdapter()).create();
            Map<String, Object> resultList = gson.fromJson(param, new TypeToken<Map<String, Object>>() {
            }.getType());
            String memberCode = resultList.get("memberCode").toString();
            String salerCode = resultList.get("salerCode").toString();
            String parentSalerCode = resultList.get("parentSalerCode").toString();
            Date dateStart = dim.parse(resultList.get("dateStart").toString());
            Date dateEnd =  dim.parse(resultList.get("dateEnd").toString());
            Integer pageNo = Integer.parseInt( resultList.get("pageNo").toString());
            Integer pageSize = Integer.parseInt( resultList.get("pageSize").toString());
            checkQueryAccountBalance(memberCode,parentSalerCode);
            result = ejzhmxService.accountDetailsQuery(salerCode,parentSalerCode, dateStart, dateEnd, pageNo, pageSize);
        } catch (AccountBalanceException e) {
            exception = e.getMessage();
            result = "{\"code\":\"1\",\"flag\":\"1\",\"message\":\"" + e.getMessage() + "\"}";
        } catch (Exception e) {
            exception = e.getMessage();
            result = "{\"code\":\"1\",\"flag\":\"1\",\"message\":\"接口调用异常!\"}";
        }
        webserviceLogService.saveServerLog(
                AccountBalanceService.class.getName(),
                "accountDetailsQuery",
                param,
                "返回结果：" + result,
                exception,
                ""
        );
        return result;
    }

    private void checkAccountBalance(String EsbCode, String salerCode,String parentSalerCode, Long localSerialNumber, Long unLocalSerialNumber) {
        if (localSerialNumber != null && this.dfkxsdDao.getlocalSerialNumberOver(localSerialNumber)) {
            throw new AccountBalanceException("非法数据");
        }
        if (unLocalSerialNumber != null && this.dfkxsdDao.getUnlocalSerialNumberOver(unLocalSerialNumber)) {
            throw new AccountBalanceException("非法数据");
        }
        if (EsbCode == null) {
            throw new AccountBalanceException("电子超市客户Esbcode为空");
        }
        if (salerCode == null) {
            throw new AccountBalanceException("代理客户salerCode为空");
        }
        if (parentSalerCode == null) {
            throw new AccountBalanceException("赎货专场卖家parentSalerCode为空");
        }
        Gsjg dzs = gsjgService.findKhByEsbcode(EsbCode);
        if (dzs == null) {
            throw new AccountBalanceException("电子超市客户为空");
        }
        Gsjg dlkh = gsjgService.findKhByEsbcode(salerCode);
        if (dlkh == null) {
            throw new AccountBalanceException("代理客户为空");
        }
        Gsjg shzcmj = gsjgService.findKhByEsbcode(parentSalerCode);
        if (shzcmj == null) {
            throw new AccountBalanceException("赎货专场卖家为空");
        }
        Ejzh dzszh = this.ejzhDao.findKyyeByKhId(dzs.getId(),shzcmj.getId());
        if (dzszh == null) {
            throw new AccountBalanceException("电子超市客户没有对应的二级账户");
        }
        Ejzh dlkhzh = this.ejzhDao.findKyyeByKhId(dlkh.getId(),shzcmj.getId());
        if (dlkhzh == null) {
            throw new AccountBalanceException("代理客户没有对应的二级账户");
        }
    }

    private void checkQueryAccountBalance(String EsbCode,String parentSalerCode) {
        if (EsbCode == null) {
            throw new AccountBalanceException("电子超市客户Esbcode为空");
        }
        if (parentSalerCode == null) {
            throw new AccountBalanceException("赎货专场卖家parentSalerCode为空");
        }
        Gsjg dzs = gsjgService.findKhByEsbcode(EsbCode);
        if (dzs == null) {
            throw new AccountBalanceException("电子超市客户为空");
        }
        Gsjg shzcmj = gsjgService.findKhByEsbcode(parentSalerCode);
        if (shzcmj == null) {
            throw new AccountBalanceException("赎货专场卖家为空");
        }
        Ejzh dzszh = this.ejzhDao.findKyyeByKhId(dzs.getId(),shzcmj.getId());
        if (dzszh == null) {
            throw new AccountBalanceException("电子超市客户没有对映的二级账户");
        }
    }


    @Autowired
    public void setEjzhService(EjzhService ejzhService) {
        this.ejzhService = ejzhService;
    }

    @Autowired
    public void setEjzhmxService(EjzhmxService ejzhmxService) {
        this.ejzhmxService = ejzhmxService;
    }

    @Autowired
    public void setDfkxsdDao(DfkxsdDao dfkxsdDao) {
        this.dfkxsdDao = dfkxsdDao;
    }

    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }

    @Autowired
    public void setEjzhDao(EjzhDao ejzhDao) {
        this.ejzhDao = ejzhDao;
    }

    @Autowired
    public void setWebserviceLogService(WebserviceLogService webserviceLogService) {
        this.webserviceLogService = webserviceLogService;
    }
}
